#include "DustSensor.h"

DustSensor GPY;
long startTime = 0;
//String inStr = "";

void setup(){
	Serial.begin(38400);
	GPY.Init(9, A0);
	delay(1000);
	startTime = millis();
}

void loop()
{
	GPY.Sample();

	if (millis() - startTime > 2000)
	{
		GPY.DustDataUpdate();
		Serial.print("Dust Density: ");
		Serial.print(GPY.dustDensity * 1000, 2);
		Serial.println(" ug/m3");

		Serial.print("Voltage: ");
		Serial.print(GPY.voltage);
		Serial.println(" V");

		Serial.print("ppmPercf: ");
		Serial.println(GPY.ppmPercf, 2);
		Serial.println("");
		startTime = millis();
	}

	if (Serial.available())
	{		
		Serial.println(Serial.readString());
	}
	//Serial.println("");
}






/*
Interface to Sharp GP2Y1010AU0F Particle Sensor
Program by Christopher Nafis
Written April 2012

http://www.sparkfun.com/datasheets/Sensors/gp2y1010au_e.pdf
http://sensorapp.net/?p=479

Sharp pin 1 (V-LED)   => 5V (connected to 150ohm resister)
Sharp pin 2 (LED-GND) => Arduino GND pin
Sharp pin 3 (LED)     => Arduino pin 2
Sharp pin 4 (S-GND)   => Arduino GND pin
Sharp pin 5 (Vo)      => Arduino A0 pin
Sharp pin 6 (Vcc)     => 5V
*/

//#include <stdlib.h>
//
//const int postingInterval = 2000;  //delay between updates to Pachube.com
//
//int dustPin = A0;
//int ledPower = 9;
//int delayTime = 280;
//int delayTime2 = 40;
//float offTime = 9680;
//
//long startTime = 0;
//
//int dustVal = 0;
//int i = 0;
//float ppm = 0;
//char s[32];
//float voltage = 0;
//float dustdensity = 0;
//float ppmpercf = 0;
//
//void setup(){
//	Serial.begin(9600);
//	pinMode(ledPower, OUTPUT);
//
//	// give the ethernet module time to boot up:
//	delay(1000);
//	i = 0;
//	ppm = 0;
//
//	startTime = millis();
//}
//
//void loop(){
//	i = i + 1;
//	digitalWrite(ledPower, LOW); // power on the LED
//	delayMicroseconds(delayTime);
//	dustVal = analogRead(dustPin); // read the dust value
//	ppm = ppm + dustVal;
//	delayMicroseconds(delayTime2);
//	digitalWrite(ledPower, HIGH); // turn the LED off
//	delayMicroseconds(offTime);
//
//	// if you're not connected, and ten seconds have passed since
//	// your last connection, then connect again and send data:
//	if (millis() - startTime > postingInterval) {
//		voltage = ppm / i*0.0049;
//		dustdensity = 0.17*voltage - 0.1;
//		ppmpercf = (voltage - 0.0256) * 120000;
//		if (ppmpercf < 0)
//			ppmpercf = 0;
//		if (dustdensity < 0)
//			dustdensity = 0;
//		if (dustdensity > 0.5)
//			dustdensity = 0.5;
//		String dataString = "";
//		dataString += dtostrf(voltage, 9, 4, s);
//		dataString += ",";
//		dataString += dtostrf(dustdensity, 5, 2, s);
//		dataString += ",";
//		dataString += dtostrf(ppmpercf, 8, 0, s);
//		i = 0;
//		ppm = 0;
//		Serial.println(dataString);
//		startTime = millis();
//	}
//}